package guo.controller;

import guo.util.FileToMultiFile;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import java.io.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

/**
 * @author: gsq
 * @description: TODO
 * @date: 2024/5/13 9:48
 * @version: 1.0
 */
@RestController
public class UnZipController {

    @PostMapping("/file")
    public void unZip(MultipartFile file) throws IOException {

//        new HashMap<String,MultipartFile>()

        if (file == null || file.getSize() <= 0){
//            return AjaxResult.error("上传文件为空")
        }

        String fileName = file.getOriginalFilename();
        if (!"zip".equals(fileName.substring(fileName.lastIndexOf(".")+1))){
//            return AjaxResult.error(300,"请上传zip格式的压缩包");
        }

        ZipEntry zipEntry = null;
        ZipInputStream zipInputStream = new ZipInputStream(file.getInputStream());
        while ((zipEntry = zipInputStream.getNextEntry()) != null){

//  1.将zip文件条目转为 file
            File tempFile = File.createTempFile("temp", null);
            tempFile.deleteOnExit();

            // 写入条目内容到临时文件
            FileOutputStream fos = new FileOutputStream(tempFile);
            byte[] content = new byte[(int) zipEntry.getSize()];
            int bytesRead;
            while ((bytesRead = zipInputStream.read(content)) != -1) {
                fos.write(content, 0, bytesRead);
            }
            fos.close();
            MultipartFile multipartFile = FileToMultiFile.getMultipartFile(tempFile);



//  得到当前文件名  判断是xlsx还是jpg文件
            String filename1 = zipEntry.getName();
            String substring = filename1.substring(filename1.lastIndexOf(".")+1);
            if (substring != null && substring.equals("xlsx")){

            }else if (substring != null && (substring.equals("jpg") || substring.equals("png"))){

            }else {
                throw new RuntimeException("zip文件格式错误，导入失败");
            }

//  若是xlsx文件，将数据得到list集合

//  若是jpg/png文件，根据存储的list集合对应的图片名，存储

            if (filename1.endsWith("/") || filename1.endsWith("\\")){
                System.out.println("存在空文件夹，文件夹名为："+zipEntry.getName());
                continue;
            }

//  得到图片名称带后缀
            String[] split ;
            String photoName = null;
            if (filename1.contains("/")){
                split = filename1.split("/");
                photoName = split[split.length-1];
            }else if (filename1.contains("\\")){
                split = filename1.split("\\\\");
                photoName = split[split.length-1];
            }else {
                photoName = filename1;
            }

//  得到图片名 不带后缀
            String idCard = photoName.substring(0,photoName.indexOf("."));
            System.out.println(" 文件名："+photoName);
            if (StringUtils.isEmpty(photoName)){
                throw new RuntimeException("获取文件名错误");
            }

//  得到图片后缀名 jpg/png
            String suffix = photoName.substring(photoName.lastIndexOf(".") + 1);
            if (!("jpg".equalsIgnoreCase(suffix) || "png".equalsIgnoreCase(suffix))){
                throw new RuntimeException("压缩包存在不符合图片格式（jpg和png）的文件");
            }




        }
    }
}
